package com.vogue.supplymall.cross.domain.prodiver;

import com.vogue.ssm.database.provider.BaseSqlProvider;
import com.vogue.supplymall.common.domain.dto.admin.ShopSearchInfo;
import com.vogue.supplymall.cross.domain.prodiver.tools.OrderUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * The type Not open shop sql provider.
 * Create in 2018-05-15
 * @author  zhangliang
 */
public class NotOpenShopSqlProvider extends BaseSqlProvider {
    /**
     * Find string.
     *
     * @param shopSearchInfo the shop search info
     * @return the string
     */
    public String find(ShopSearchInfo shopSearchInfo) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ")
                .append(" la.id, ")
                .append(" la.shopid, ")
                .append(" la.syscodeid, ")
                .append(" la.matchid, ")
                .append(" la.scopeid, ")
                .append(" la.applystatue, ")
                .append(" la.applytype, ")
                .append(" la.checkdate, ")
                .append(" s.name, ")
                .append(" s.opendate, ")
                .append(" GROUP_CONCAT(CONCAT(lad.licencetypeid,';',lad.id,';',lad.begindate,';',lad.enddate,';',lad.applystatue,';',lad.url)) liu ");
        sql.append(" FROM ")
                .append(" licence_apply la ")
                .append(" LEFT JOIN licence_apply_detail lad ON (la.id = lad.applyid) ")
                .append(" LEFT JOIN shop s on(la.shopid = s.id) ");
        sql.append(" WHERE ")
                .append(" la.syscodeid = #{syscodeid} ")
                .append(" AND la.disabledflag = 0 ")
                .append(" AND lad.disabledflag = 0 ");
        if (StringUtils.isNotEmpty(shopSearchInfo.getShopName())) {
            sql.append(" and s.`name` LIKE CONCAT('%',#{shopName},'%') ");
        }
        if (null != shopSearchInfo.getShopState()) {
            sql.append(" and la.applystatue = #{shopState} ");
        }
        if (null != shopSearchInfo.getScopeId()) {
            sql.append(" and la.scopeid = #{scopeId} ");
        }
        if (null != shopSearchInfo.getLicenceState()) {
            sql.append(" and lad.applystatue = #{licenceState} ");
        }
        if (null != shopSearchInfo.getLicenceTypeId()) {
            sql.append(" and lad.licencetypeid = #{licenceTypeId} ");
        }
        sql.append(" GROUP BY id ");
        sql.append(OrderUtils.orderBy(shopSearchInfo));
        sql.append(" LIMIT #{start},#{length}");


        return sql.toString();
    }


    /**
     * Find string.
     *
     * @param shopSearchInfo the shop search info
     * @return the string
     */
    public String findCount(ShopSearchInfo shopSearchInfo) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ")
                .append(" count(DISTINCT la.id) ");
        sql.append(" FROM ")
                .append(" licence_apply la ")
                .append(" LEFT JOIN licence_apply_detail lad ON (la.id = lad.applyid) ")
                .append(" LEFT JOIN shop s on(la.shopid = s.id) ");
        sql.append(" WHERE ")
                .append(" la.syscodeid = #{syscodeid} ")
                .append(" AND la.disabledflag = 0 ")
                .append(" AND lad.disabledflag = 0 ");
        if (StringUtils.isNotEmpty(shopSearchInfo.getShopName())) {
            sql.append(" and s.`name` LIKE CONCAT('%',#{shopName},'%') ");
        }
        if (null != shopSearchInfo.getShopState()) {
            sql.append(" and la.applystatue = #{shopState} ");
        }
        if (null != shopSearchInfo.getScopeId()) {
            sql.append(" and la.scopeid = #{scopeId} ");
        }
        if (null != shopSearchInfo.getLicenceState()) {
            sql.append(" and lad.applystatue = #{licenceState} ");
        }
        if (null != shopSearchInfo.getLicenceTypeId()) {
            sql.append(" and lad.licencetypeid = #{licenceTypeId} ");
        }
        return sql.toString();
    }
}
